import os
from urllib.error import URLError


def main():
    url = "https://movie.douban.com/top250?start="
    datalist = getData(url)
    savepath = "./电影.xlsx"


import urllib.request as url
import urllib.parse
from bs4 import BeautifulSoup


def getData(baseUrl):
    datalist = []
    for i in range(0, 10):
        url = baseUrl + str(i * 25)
        print(url)
        # 获取网页
        html = askUrl(url)
        # 开始循环解析网页
        # 文档解析  bs4
        # file = open("./1.html", "rb")
        # html = file.read()
        bs = BeautifulSoup(html, "html.parser")
        print(bs.text)
    # 逐条解析
    return datalist


# 解析url
def askUrl(url):
    # get 请求
    # response = url.urlopen(baseUrl)  timeout=?  超时处理

    # post请求
    # data = bytes(urllib.parse.urlencode({"1449624007": "hello"}), encoding="utf-8")
    try:
        # 模拟浏览器头部信息
        headers = {
            "User-Agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 92.0.4515.159Safari / 537.36Edg / 92.0.902.78"
        }
        req = urllib.request.Request(url, headers=headers)
        print(type(req))
        response = urllib.request.urlopen(req, timeout=1)
        html = response.read().decode("utf-8")

        return html
    except (AttributeError, URLError) as exception:
        print(exception)


# 保存数据
def saveData(savepath):
    pass


if __name__ == "__main__":
    main()
